package com.yixun.qfbao.hander;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Model;
import com.yixun.qfbao.model.ModelInfo;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.enums.ModelTypeEnum2;
import com.yixun.qfbao.service.ModelInfoService;
import com.yixun.qfbao.service.ModelService;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.UpdateCompanyCredentialReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * @author zilong.liu
 * @date 2020-12-16
 *
 * desc:
 *     	获取公司注册列表
 */
@Service("updateCompanyCredentialHander")
public class UpdateCompanyCredentialHander extends CommonAbstractHander{

	@Autowired
	LoginUtils loginUtils;
	
	@Autowired
	ModelInfoService modelInfoService;
	
	@Autowired
	ModelService modelService;
	
	@Override
	@SuppressWarnings({ "rawtypes"})
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		BaseResponseDTO<String> res = new BaseResponseDTO<>();
		
		UpdateCompanyCredentialReqVo reqVo = (UpdateCompanyCredentialReqVo) reqDto;
		String userId = reqVo.getParams().getUserId();
		PartnerInfo partnerInfo = loginUtils.getUser(userId);
		if(partnerInfo == null) {
			res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
		}
		ModelInfo modelInfo = new ModelInfo();
		modelInfo.setRelId(partnerInfo.getHigherid());
		modelInfo.setModelType(ModelTypeEnum2.COMPANY.getNo());
		modelInfo.setModelName("营业执照");
		modelInfoService.delByDto(modelInfo);
		
		Model model = new Model();
		model.setCompanyId(partnerInfo.getHigherid());
		model.setType(ModelTypeEnum2.COMPANY.getNo());
		model.setName("营业执照");
		List<Model> modelList = modelService.selectListByDto(model );
		
		String companyCredential = reqVo.getParams().getCompanyCredential();
		List<ModelInfo> modelInfoList = new ArrayList<>();
		modelInfo.setModelId(modelList.get(0).getId());
		modelInfo.setModelValue(companyCredential);
		modelInfoList.add(modelInfo);
		modelInfoService.batchInsertModelInfo(modelInfoList);
		
		res.setResult(ResultEnum.SUCCESS.getNo());
		return res;
	}

}
